'use strict';
const glob = require('glob');
const path = require('path');
const webpack = require('webpack');
const chalk = require('chalk');
const os = require('os');

const HappyPack = require('happypack'); //多进程管理提高打包速度
const ProgressBarPlugin = require('progress-bar-webpack-plugin'); //构建进度条
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');//合并重复的css
const happyThreadPool = HappyPack.ThreadPool({
    size: os.cpus().length
});
const setMPA = require('./getEntry');//统一获取入口文件

const {entry} = setMPA(); //获取入口文件

// 基础打包配置
module.exports = {
    context: path.resolve(__dirname, "../src"),
    entry: entry,
    output: {
        publicPath:'/public/',
        filename: "[name].js",
        path: path.resolve(__dirname, "../dist")
    },
    mode: "development",
    resolve: {
        extensions: ['.js', '.mjs', '.jsx', '.json', '.tsx', '.ts'],
    },
         


    module: {
        rules: [{
                test: /.jsx?$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: "happypack/loader?id=babel",
                }
            },
            {
                test: /.tsx?$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: "ts-loader",
                }
            },


        ]
    },
    plugins: [

        new HappyPack({
            id: 'babel',
            loaders: [{
                loader: "babel-loader",
                options: {
                    cacheDirectory: true,
                }
            }],
            threadPool: happyThreadPool
        }),
        // 使用多进程提高打包速度
        // new HappyPack({
        //   id: 'ts',
        //   loaders: [{
        //       loader:"ts-loader",
        //       options:{

        //       }
        //     }],
        //   threadPool: happyThreadPool
        // }),
        // 单独打包webpack

        // 自动加载模块，省去import 或 require
        new webpack.ProvidePlugin({
            React: 'react',
            ReactDOM: 'react-dom',
            fetch: 'isomorphic-fetch',
            promise: 'promise',
            axios: 'axios'
        }),


        // 编译进度条
        new ProgressBarPlugin({
            format: '  build [:bar] ' + chalk.green.bold(':percent') + ' (:elapsed seconds)'
        }),
        new OptimizeCssAssetsPlugin({
            // assetNameRegExp: /\.optimize\.css$/g,
            cssProcessor: require('cssnano'),
            cssProcessorPluginOptions: {
              preset: ['default', { discardComments: { removeAll: true } }],
            },
            canPrint: true
          })

    ]

}

 